# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=doxygen
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.8.14
pkgrel=2
pkgdesc="A documentation system for C++, C, Java, IDL and PHP (mingw-w64)"
arch=('any')
url="http://www.doxygen.org/"
options=('strip' 'staticlibs')
license=('GPL')
depends=(#"${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-xapian-core")
makedepends=(#"${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             #"${MINGW_PACKAGE_PREFIX}-polly"
             "${MINGW_PACKAGE_PREFIX}-python2"
             #"${MINGW_PACKAGE_PREFIX}-qt5"
             'flex'
             'bison')
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5")
source=("https://ftp.stack.nl/pub/users/dimitri/${_realname}-${pkgver}.src.tar.gz"
        'cmake-mingw.patch'
        'fix-casts.patch')
sha256sums=('d1757e02755ef6f56fd45f1f4398598b920381948d6fcfa58f5ca6aa56f59d4d'
            'c760a8d583c1d27ef7dbdc7e43ec6bc9d4af8ead6fdb95d539b4857f2c41dbf6'
            '0cbf92d80e757287bfe1bb86168751de8a467e31e8ed26baf7c2b75450ce1f4c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/cmake-mingw.patch"
  patch -p1 -i "${srcdir}/fix-casts.patch"
}

build() {
  export QTDIR=${MINGW_PREFIX}

  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -Dbuild_wizard=ON \
    -Dbuild_search=ON \
    -Duse_sqlite3=ON \
    -Duse_libclang=OFF \
    -Dbuild_parse=ON \
    ../${_realname}-${pkgver}

  # fix some generated makefiles: replace LLVM-NOTFOUND by the path to LLVM.dll
  llvm_path_mixed=$(cygpath -m `which LLVM.dll`)
  llvm_path_unix=$(echo ${llvm_path_mixed} | sed 's|C:|/C|')
  sed -e "s|\(bin/.*\.exe: \)LLVM-NOTFOUND|\1${llvm_path_mixed}|" \
      -e "s|\(.*libclangBasic.a \)LLVM-NOTFOUND|\1${llvm_path_unix}|" \
      -i ./addon/doxyparse/CMakeFiles/doxyparse.dir/build.make \
         ./src/CMakeFiles/doxygen.dir/build.make

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  make DESTDIR="${pkgdir}" install
}
